package com.smm.service;

import com.jn.sqlhelper.dialect.pagination.PagingRequest;
import com.jn.sqlhelper.dialect.pagination.PagingRequestContextHolder;
import com.jn.sqlhelper.dialect.pagination.PagingResult;
import com.jn.sqlhelper.dialect.pagination.SqlPaginations;
import com.smm.dao.UserDao;
import com.smm.entity.User;
import com.smm.util.SHA1Util;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {
    private Logger log = LoggerFactory.getLogger(UserService.class);
    @Resource(name = "userDao")
    private UserDao userDao;

    public User login(User user) {
        User findUser = userDao.findUser(user);
        if (findUser != null && SHA1Util.validate(user.getPassword(), findUser.getPassword())) {
            return findUser;
        } else {
            User user1 = new User();
            user1.setStatus(false);
            return user1;
        }

    }

    public Integer addUser(User user) {
        User findUser = userDao.findUser(user);
        if (findUser == null) {
            user.setPassword(new Md5Hash(user.getPassword(), user.getUsername(), 5).toString());
            //user.setPassword(SHA1Util.sha1(user.getPassword()));
            return userDao.addUser(user);
        } else {
            return null;
        }
        //user.setPassword(new Md5Hash(user.getPassword(), user.getUsername(), 5).toString());
        //return userDao.addUser(user);

    }

    public Integer updateUser(User user) {

        if (user.getPassword() != null && !user.getPassword().equals("")) {
            user.setPassword(new Md5Hash(user.getPassword(), user.getUsername(), 5).toString());
        }
        return userDao.updateUser(user);
    }
    //@CacheEvict(value = "user",key = "#id")
    public Integer deleteUser(Integer id) {
        return userDao.deleteUser(id);
    }

    public User findUserById(Integer id) {
        User user = new User();
        user.setId(id);
        return userDao.findUser(user);
    }
    //@Cacheable(value = "user",key = "#user.id")
    public User findUser(User user) {
        return userDao.findUser(user);
    }

    public PagingResult findUserPage(Integer pageSize, Integer pageNumber, User user) {
        //PagingRequest request = new PagingRequest().setPageNo(pageNumber).setPageSize(pageSize);
        PagingRequest PagingRequest = SqlPaginations.preparePagination(pageNumber, pageSize);
        PagingRequestContextHolder.getContext().setPagingRequest(PagingRequest);
        List<User> users = userDao.findUserPage(user);
        return PagingRequest.getResult().setItems(users);
    }

    public User getCollect(Integer id) {
        return userDao.getCollectByUserId(id);
    }


}
